package com.atguigu.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {

    public static void main(String[] args) throws IOException {

        FileInputStream fileInputStream = new FileInputStream("file01.txt");

        FileChannel inputStreamChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("file03.txt");

        FileChannel outputStreamChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((5));

        while (true) {

            byteBuffer.clear();

            int read = inputStreamChannel.read(byteBuffer);
            System.out.println("read=  " + read);
            if (read == -1) {
                break;
            }

            byteBuffer.flip();

            outputStreamChannel.write(byteBuffer);
        }


        // 将byteBuffer 写入到当前channel
        fileInputStream.close();
        fileOutputStream.close();

    }
}
